export default {
  // 获取标准格式时间
  getTime() {
    let d = new Date()
    let year = d.getFullYear()
    let month = d.getMonth()
    month = month + 1 > 12 ? 1 : month + 1
    month = month > 9 ? month : '0' + month.toString()
    let day = d.getDate()
    day = day > 9 ? day : '0' + day.toString()
    let hour = d.getHours()
    // eslint-disable-next-line no-unused-vars
    hour = hour > 9 ? hour : '0' + hour.toString()
    let minute = d.getMinutes()
    // eslint-disable-next-line no-unused-vars
    minute = minute > 9 ? minute : '0' + minute.toString()
    let second = d.getSeconds()
    // eslint-disable-next-line no-unused-vars
    second = second > 9 ? second : '0' + second.toString()
    // return `${year}-${month}-${day} ${hour}:${minute}:${second}`
    return `${year}-${month}-${day}`
  },
  getTimeOnly(){
	let d = new Date()
	let hour = d.getHours()
	// eslint-disable-next-line no-unused-vars
	hour = hour > 9 ? hour : '0' + hour.toString()
	let minute = d.getMinutes()
	// eslint-disable-next-line no-unused-vars
	minute = minute > 9 ? minute : '0' + minute.toString()
	let second = d.getSeconds()
	// eslint-disable-next-line no-unused-vars
	second = second > 9 ? second : '0' + second.toString()
	return `${hour}:${minute}`
  },
  getStandardTime() {
    let d = new Date()
    let year = d.getFullYear()
    let month = d.getMonth()
    month = month + 1 > 12 ? 1 : month + 1
    month = month > 9 ? month : '0' + month.toString()
    let day = d.getDate()
    day = day > 9 ? day : '0' + day.toString()
    let hour = d.getHours()
    // eslint-disable-next-line no-unused-vars
    hour = hour > 9 ? hour : '0' + hour.toString()
    let minute = d.getMinutes()
    // eslint-disable-next-line no-unused-vars
    minute = minute > 9 ? minute : '0' + minute.toString()
    let second = d.getSeconds()
    // eslint-disable-next-line no-unused-vars
    second = second > 9 ? second : '0' + second.toString()
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`
    // return `${year}-${month}-${day}`
  },
  judgeWeek(time) {
    let days = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
    let dateParts = time.split('-')
    let year = parseInt(dateParts[0])
    let month = parseInt(dateParts[1]) - 1 // 月份从0开始计数
    let day = parseInt(dateParts[2])
    let date = new Date(year, month, day)
    let dayOfWeek = date.getDay() // 返回值为0（周日）到6（周六）
    return days[dayOfWeek]
  },
  // 传入yyyy-mm-dd，如果是当天，则显示“今天”，如果不是当天，则显示"mm-dd"
  getDayOfWeek(time) {
    if (time.slice(0, 10) === this.getTime()) {
      return '今天'
    } else {
      return time.slice(5, 10)
    }
  },
  // 根据时间处理数组，得到前端展示的数据
  arrayDealByTime(arr,timeColName) {
    arr.forEach(item => {
      if (item[timeColName]) {
        item.week = this.judgeWeek(item.createT)
        item.mmdd = this.getDayOfWeek(item.createT)
        item[timeColName] = item[timeColName].slice(0, 10)
      } else {
        item.week = ''
        item.mmdd = ''
        item[timeColName] = ''
      }
    })
    return arr
  },
  // 按时间升序排列,参数1：数组，参数2：需要按时间排序的属性名
  timeAsc(arr, timeColName) {
    arr.sort(function (a, b) {
      return new Date(a[timeColName]) - new Date(b[timeColName])
    })
    return arr
  },
  // 按时间降序排列,参数1：数组，参数2：需要按时间排序的属性名
  timeDesc(arr, timeColName) {
    arr.sort(function (a, b) {
      return new Date(b[timeColName]) - new Date(a[timeColName])
    })
    return arr
  },
  // 传入数组，根据时间排序和归类
  arraySortByTime(arr) {
    let result = []
    arr.forEach(item => {
      let date = item.createT
      let week = item.week
      let mmdd = item.mmdd
      let found = result.find(element => {
        return element.date === date
      })
      if (found) {
        found.list.push(item)
      } else {
        result.push({
          date: date,
          week: week,
          mmdd: mmdd,
          list: [item]
        })
      }
    })
    result = this.timeDesc(result, 'date')
    return result
  },
  // 根据系统时间返回不同的问候语
  greeting() {
    let currentTime = new Date()
    let currentHour = currentTime.getHours()
    let greeting = ''
    if (currentHour < 5) {
      greeting = '深夜了，请注意休息'
    } else if (currentHour < 12) {
      greeting = '早上好'
    } else if (currentHour < 14) {
      greeting = '中午好'
    } else if (currentHour < 18) {
      greeting = '下午好'
    } else {
      greeting = '晚上好'
    }
    return greeting
  },
  // 判断某个时间是否在2个时间之间
  checkAuditTime(sTime,eTime){
	  let nowTime = new Date();
	  let dataStr = this.getTime();
	  // 获取开始时间、结束时间、现在时间的时间戳
		let startDate = new Date(dataStr+' '+sTime).getTime();
		let endDate = new Date(dataStr+' '+eTime).getTime();
		let nowDate = nowTime.getTime();
	  
	   // 判断现在的时间是否在开始时间和结束时间之间，若s为true则结果取反
		if (nowDate >= startDate && nowDate <= endDate) {
		  return true;
		} else {
		  return false;
		}
  },
  // 判断某个时间是否在某个时间后
   isAfterTime(eTime){
	    let nowTime = new Date();
		let dataStr = this.getTime();
		let endTime = new Date(dataStr+' '+eTime).getTime();
		let nowDate = nowTime.getTime();
		if(nowDate>endTime){
			// 结束了
			return true;
		}
   },
   // 判断某个时间是否在某个时间前
    getBeforeTime(sTime){
   	    let nowTime = new Date();
   		let dataStr = this.getTime();
   		let beginTime = new Date(dataStr+' '+sTime).getTime();
   		let nowDate = nowTime.getTime();
		
   		if(nowDate<beginTime){
   			return beginTime-nowDate;  // 返回倒计时时间
   		} else {
			return false
		}
    }
}
